package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.YonbipFileUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 自定义档案Controller
 * 
 * @author ruoyi
 * @date 2025-09-08
 */
@Controller
@RequestMapping("/system/defdoc")
public class BdDefdocController extends BaseController
{
    private String prefix = "system/defdoc";

    @Autowired
    private IBdDefdocService bdDefdocService=SpringUtils.getBean(IBdDefdocService.class);;
    private IYonbipBdDefdoclistService iYonbipBdDefdoclistService = SpringUtils.getBean(IYonbipBdDefdoclistService.class);
    private IOrgAccbookService orgAccbookService= SpringUtils.getBean(IOrgAccbookService.class);
    private IOrgCqJkService orgCqJkService=SpringUtils.getBean(IOrgCqJkService.class);

    @RequiresPermissions("system:defdoc:view")
    @GetMapping()
    public String defdoc()
    {
        return prefix + "/defdoc";
    }

    /**
     * 查询自定义档案列表
     */
    @RequiresPermissions("system:defdoc:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BdDefdoc bdDefdoc)
    {
        startPage();
        List<BdDefdoc> list = bdDefdocService.selectBdDefdocList(bdDefdoc);
        return getDataTable(list);
    }

    /**
     * 导出自定义档案列表
     */
    @RequiresPermissions("system:defdoc:export")
    @Log(title = "自定义档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BdDefdoc bdDefdoc)
    {
        List<BdDefdoc> list = bdDefdocService.selectBdDefdocList(bdDefdoc);
        ExcelUtil<BdDefdoc> util = new ExcelUtil<BdDefdoc>(BdDefdoc.class);
        return util.exportExcel(list, "自定义档案数据");
    }

    /**
     * 新增自定义档案
     */
    @RequiresPermissions("system:defdoc:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存自定义档案
     */
    @RequiresPermissions("system:defdoc:add")
    @Log(title = "自定义档案", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BdDefdoc bdDefdoc)
    {
        return toAjax(bdDefdocService.insertBdDefdoc(bdDefdoc));
    }

    /**
     * 修改自定义档案
     */
    @RequiresPermissions("system:defdoc:edit")
    @GetMapping("/edit/{pkDefdoc}")
    public String edit(@PathVariable("pkDefdoc") String pkDefdoc, ModelMap mmap)
    {
        BdDefdoc bdDefdoc = bdDefdocService.selectBdDefdocByPkDefdoc(pkDefdoc);
        mmap.put("bdDefdoc", bdDefdoc);
        return prefix + "/edit";
    }

    /**
     * 修改保存自定义档案
     */
    @RequiresPermissions("system:defdoc:edit")
    @Log(title = "自定义档案", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BdDefdoc bdDefdoc)
    {
        return toAjax(bdDefdocService.updateBdDefdoc(bdDefdoc));
    }

    /**
     * 删除自定义档案
     */
    @RequiresPermissions("system:defdoc:remove")
    @Log(title = "自定义档案", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bdDefdocService.deleteBdDefdocByPkDefdocs(ids));
    }
    public void ApiQueryDefDoc(){


        try
        {

            List<OrgAccbook> orgAccbooklist= orgAccbookService.selectOrgAccbookList(new OrgAccbook());

            for(OrgAccbook orgAccbook :orgAccbooklist){
                OrgCqJk orgCqJk=orgCqJkService.selectOrgCqJkByCode(orgAccbook.getCODE());
                if(StringUtils.isNull(orgCqJk)){
                    continue;
                }

                List<YonbipBdDefdoclist> yonbipBdDefdoclist=iYonbipBdDefdoclistService.selectYonbipBdDefdoclistList(new YonbipBdDefdoclist());
                for(YonbipBdDefdoclist yonbipBdDefdoclist1 : yonbipBdDefdoclist ){
                    List<String> lines=new ArrayList<>();
                    List<String> linesSb=new ArrayList<>();
                    //成功数
                    int cnum=0;
                    //失败数
                    int snum=0;

                    BdDefdoc bdDefdoc=new BdDefdoc();
                    bdDefdoc.setPkDefdoclist(yonbipBdDefdoclist1.getPkDefdoclist());
                    bdDefdoc.setPkOrg(orgAccbook.getPkRelorg());
                    List<BdDefdoc> list = bdDefdocService.selectBdDefdocList(bdDefdoc);
                    for(BdDefdoc bdDefdoc1:list){
                        lines.add(bdDefdoc1.getCODE()+"|"+bdDefdoc1.getNAME()+"|"+orgCqJk.getJcode()) ;
                        cnum++;
                    }
                    YonbipFileUtils.createFileWithLines(
                            "C:/basedata",
                            "20251029"+"-"+orgCqJk.getJcode()+"-"+yonbipBdDefdoclist1.getCode()+".txt",
                            lines,
                            false
                    );
                    linesSb.add("自定义档案数据导入成功"+cnum+"条");
                    linesSb.add("自定义档案数据导入失败"+snum+"条");
                    YonbipFileUtils.createFileWithLines(
                            "C:/basedata",
                            "20251029"+"-"+orgCqJk.getJcode()+"-"+yonbipBdDefdoclist1.getCode()+".txt.ok",
                            linesSb,
                            false
                    );

                }
            }


        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    // 查询商品名称
    public void ApiQueryMaterial(){


        try
        {

            List<OrgAccbook> orgAccbooklist= orgAccbookService.selectOrgAccbookList(new OrgAccbook());

            for(OrgAccbook orgAccbook :orgAccbooklist){
                OrgCqJk orgCqJk=orgCqJkService.selectOrgCqJkByCode(orgAccbook.getCODE());
                if(StringUtils.isNull(orgCqJk)){
                    continue;
                }

                    List<String> lines=new ArrayList<>();
                    List<String> linesSb=new ArrayList<>();
                    //成功数
                    int cnum=0;
                    //失败数
                    int snum=0;

                    BdDefdoc bdDefdoc=new BdDefdoc();
                    bdDefdoc.setPkOrg(orgAccbook.getPkRelorg());
                    List<BdDefdoc> list = bdDefdocService.selectMaterialList(bdDefdoc);
                    for(BdDefdoc bdDefdoc1:list){
                        lines.add(bdDefdoc1.getCODE()+"|"+bdDefdoc1.getNAME()+"|"+orgCqJk.getJcode()) ;
                        cnum++;
                    }
                    YonbipFileUtils.createFileWithLines(
                            "C:/basedata",
                            "20251029"+"-"+orgCqJk.getJcode()+"-"+"inventorydetail"+".txt",
                            lines,
                            false
                    );
                    linesSb.add("商品名称数据导入成功"+cnum+"条");
                    linesSb.add("商品名称数据导入失败"+snum+"条");
                    YonbipFileUtils.createFileWithLines(
                            "C:/basedata",
                            "20251029"+"-"+orgCqJk.getJcode()+"-"+"inventorydetail"+".txt.ok",
                            linesSb,
                            false
                    );


            }


        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
